<cfcomponent extends="assets.calgrants.com.Base">

	<cfscript>
        import assets.calgrants.com.bo.User;
	</cfscript>
	
	<cffunction name="get" access="public" output="false" returntype="assets.calgrants.com.bo.User">
		<cfargument name="User_ID" type="numeric" required="true" />
		<cfscript>
			if (arguments.User_ID gt 0) {
				local.loadArgs = {};
				local.loadArgs.obj = new User();
				StructAppend(local.loadArgs, QueryToStruct(read(argumentCollection = arguments))[1]);
				return load(argumentCollection = local.loadArgs);
			} else {
				return new User();
			}
		</cfscript>
	</cffunction>
	
	<cffunction name="load" access="public" output="false" returntype="assets.calgrants.com.bo.User">
		<cfargument name="obj" type="assets.calgrants.com.bo.User" required="true" />
		<cfscript>
			local.obj = arguments.obj;
			if (IsDefined("arguments.User_ID")) {
				local.obj.setUser_ID(arguments.User_ID);
			}
			if (IsDefined("arguments.Approved")) {
				local.obj.setApproved(arguments.Approved);
			}
			if (IsDefined("arguments.Deleted")) {
				local.obj.setDeleted(arguments.Deleted);
			}
			if (IsDefined("arguments.Archived")) {
				local.obj.setArchived(arguments.Archived);
			}
			if (IsDefined("arguments.Organization_ID")) {
				local.obj.setOrganization_ID(arguments.Organization_ID); 
			}
			if (IsDefined("arguments.Organization2_ID")) {
				local.obj.setOrganization2_ID(arguments.Organization2_ID); 
			}
			if (IsDefined("arguments.Organization3_ID")) {
				local.obj.setOrganization3_ID(arguments.Organization3_ID); 
			}
			if (IsDefined("arguments.Role")) {
				local.obj.setRole(arguments.Role); 
			}
			if (IsDefined("arguments.Region_IDs")) {                             
				local.obj.setRegion_IDs(arguments.Region_IDs);
			}
			if (IsDefined("arguments.First_Name")) {
				local.obj.setFirst_Name(arguments.First_Name);
			}
			if (IsDefined("arguments.Last_Name")) {
				local.obj.setLast_Name(arguments.Last_Name);
			}
			if (IsDefined("arguments.Title")) {
				local.obj.setTitle(arguments.Title);
			}
			if (IsDefined("arguments.Email")) {
				local.obj.setEmail(arguments.Email);
			}
			if (IsDefined("arguments.Password")) {
				local.obj.setPassword(arguments.Password);
			}
			if (IsDefined("arguments.Phone")) {
				local.obj.setPhone(arguments.Phone);
			}
			if (IsDefined("arguments.Extension")) {
				local.obj.setExtension(arguments.Extension);
			}
			if (IsDefined("arguments.Login_Token")) {
				local.obj.setLogin_Token(arguments.Login_Token);
			}
			if (IsDefined("arguments.Date_Created")) {
				local.obj.setDate_Created(arguments.Date_Created);
			}
			return local.obj;
		</cfscript>
	</cffunction>

	<!--- <cfscript>
        import assets.calgrants.com.bo.User;
		this.bo = new User();
	</cfscript>
	
	<cffunction name="get" access="public" output="false" returntype="struct">
		<cfargument name="User_ID" type="numeric" required="true" />
		<cfif arguments.User_ID gt 0>
			<cfreturn QueryToStruct(read(argumentCollection = arguments))[1] />
		<cfelse>
			<cfreturn this.bo />
		</cfif>
	</cffunction> --->

	<cffunction name="read" access="public" output="false" returntype="any">
		<cfargument name="User_ID" required="false" default="" />
		<cfargument name="Approved" required="false" default="" />
		<cfargument name="Deleted" required="false" default="0" />
		<cfargument name="Archived" required="false" default="" />
		<cfargument name="Organization_ID" required="false" default="" />
		<cfargument name="Organization2_ID" required="false" default="" />
		<cfargument name="Organization3_ID" required="false" default="" />
		<cfargument name="Role" required="false" default="" />
		<cfargument name="Region_IDs" required="false" default="" />
		<cfargument name="First_Name" required="false" default="" />
		<cfargument name="Last_Name" required="false" default="" />
		<cfargument name="Title" required="false" default="" />
		<cfargument name="Email" required="false" default="" />
		<cfargument name="Password" required="false" default="" />
		<cfargument name="Phone" required="false" default="" />
		<cfargument name="Extension" required="false" default="" />
		<cfargument name="Login_Token" required="false" default="" />
		<cfargument name="Date_Created" required="false" default="" />
		<cfargument name="roles" required="false" default="" />
		<cfargument name="organizationIDs" required="false" default="" />
		<cfargument name="regionIDs" required="false" default="" />

		<cfset var recs = "">
		<cfstoredproc datasource="#getDatasource()#" procedure="readUser">
			<cfprocparam value="#arguments.User_ID#" cfsqltype="CF_SQL_INTEGER" null="#IIf(Len(Trim(arguments.User_ID)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Approved#" cfsqltype="CF_SQL_BIT" null="#IIf(Len(Trim(arguments.Approved)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Deleted#" cfsqltype="CF_SQL_BIT" null="#IIf(Len(Trim(arguments.Deleted)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Archived#" cfsqltype="CF_SQL_BIT" null="#IIf(Len(Trim(arguments.Archived)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Organization_ID#" cfsqltype="CF_SQL_INTEGER" null="#IIf(Len(Trim(arguments.Organization_ID)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Organization2_ID#" cfsqltype="CF_SQL_INTEGER" null="#IIf(Len(Trim(arguments.Organization2_ID)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Organization3_ID#" cfsqltype="CF_SQL_INTEGER" null="#IIf(Len(Trim(arguments.Organization3_ID)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Role#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.Role)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Region_IDs#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.Region_IDs)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.First_Name#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.First_Name)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Last_Name#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.Last_Name)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Title#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.Title)) eq 0, true, false)#" />
			<cfprocparam value="#LCase(arguments.Email)#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.Email)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Password#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.Password)) eq 0, true, false)#" />
			<cfprocparam value="#formatPhone(arguments.Phone)#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.Phone)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Extension#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.Extension)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Login_Token#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.Login_Token)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.Date_Created#" cfsqltype="CF_SQL_TIMESTAMP" null="#IIf(Len(Trim(arguments.Date_Created)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.roles#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.roles)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.organizationIDs#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.organizationIDs)) eq 0, true, false)#" />
			<cfprocparam value="#arguments.regionIDs#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(arguments.regionIDs)) eq 0, true, false)#" />
			<cfprocresult name="recs" />
		</cfstoredproc>
		<cfreturn recs />
	</cffunction>

	<cffunction name="save" access="public" output="false" returntype="any">
		<cfargument name="user" required="true" type="assets.calgrants.com.bo.User" />

		<cfstoredproc datasource="#getDatasource()#" procedure="saveUser">
			<cfprocparam value="#user.getUser_ID()#" cfsqltype="CF_SQL_INTEGER" variable="id" type="inout" null="false" />
			<cfprocparam value="#user.getApproved()#" cfsqltype="CF_SQL_BIT" null="false" />
			<cfprocparam value="#user.getDeleted()#" cfsqltype="CF_SQL_BIT" null="false" />
			<cfprocparam value="#user.getArchived()#" cfsqltype="CF_SQL_BIT" null="false" />
			<cfprocparam value="#user.getOrganization_ID()#" cfsqltype="CF_SQL_INTEGER" null="false" />
			<cfprocparam value="#user.getOrganization2_ID()#" cfsqltype="CF_SQL_INTEGER" null="false" />
			<cfprocparam value="#user.getOrganization3_ID()#" cfsqltype="CF_SQL_INTEGER" null="false" />
			<cfprocparam value="#user.getRole()#" cfsqltype="CF_SQL_VARCHAR" null="false" />
			<cfprocparam value="#user.getRegion_IDs()#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(user.getRegion_IDs())) eq 0, true, false)#" />
			<cfprocparam value="#user.getFirst_Name()#" cfsqltype="CF_SQL_VARCHAR" null="false" />
			<cfprocparam value="#user.getLast_Name()#" cfsqltype="CF_SQL_VARCHAR" null="false" />
			<cfprocparam value="#user.getTitle()#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(user.getTitle())) eq 0, true, false)#" />
			<cfprocparam value="#LCase(user.getEmail())#" cfsqltype="CF_SQL_VARCHAR" null="false" />
			<cfprocparam value="#user.getPassword()#" cfsqltype="CF_SQL_VARCHAR" null="false" />
			<cfprocparam value="#formatPhone(user.getPhone())#" cfsqltype="CF_SQL_VARCHAR" null="false" />
			<cfprocparam value="#user.getExtension()#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(user.getExtension())) eq 0, true, false)#" />
			<cfprocparam value="#user.getLogin_Token()#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(user.getLogin_Token())) eq 0, true, false)#" />
		</cfstoredproc>
		<cfreturn id />
	</cffunction>

	<cffunction name="destroy" access="public" output="false" returntype="any">
		<cfargument name="User_ID" required="true" />

		<cfstoredproc datasource="#getDatasource()#" procedure="destroyUser">
			<cfprocparam value="#arguments.User_ID#" cfsqltype="CF_SQL_INTEGER" null="false" />
			<cfprocparam value="true" cfsqltype="CF_SQL_BIT" null="false" />
		</cfstoredproc>
		<cfreturn true />
	</cffunction>

</cfcomponent>

